#include	<types.h>
#include	<grub.h>
#include	<system/kernel.h>

static ulong_t		flags;
//static multiboot_info_t*	mbi;

char_t	*err_codes[] =
{
	"sucess", "magic number no matching", "variables not set", "GDT not configured", "IDT not configured", "paging not set", 0
};

void	kbasic_initialize()
{

	flags = 0;
	inMain = TRUE;
	kprintf("Memory size:\n\tLower -> %d KB\n\tUpper -> %d KB\n", mbi->mem_lower, mbi->mem_upper);

	kbasic_set_flag(KF_VARS);
	kbasic_set_flag(KF_GDT);
}

void	kbasic_skip_main(long_t code)
{
	kbasic_error(code);
	inMain = FALSE;
	force_end();
}

void	kbasic_error(int_t err)
{
	switch(err)
	{
		case -1:
			kprintf("\n\t*** SYSTEM HALTED! ***");
			break;
		case 0:
			kprintf("\nFinished with sucess. \n\t*** SYSTEM HALTED! ***");
			break;
		default:
			kprintf("\nFinished because ");
			kprintf(( err > MAX_ERROR_CODES ) ? "unknown error ocurred!" : err_codes[err]);
			break;
	}
}

void	kbasic_set_flag(ulong_t	flag)
{
	flags |= flag;
}

ulong_t	kbasic_get_flags()
{
	return	flags;
}

ulong_t	kbasic_get_v2p()
{
	return	virt2phys;
}

ulong_t	kbasic_get_ex_mem()
{
	return	mbi->mem_upper;
}

void*	virtual2physical(void* addr)
{
	return (void*)addr+(long_t)virt2phys;	// addr + ((long_t)load_adr - (long_t)load_adr_v);
}

void*	physical2virtual(void* addr)
{
	return (void*)(ulong_t)(addr-virt2phys);
}

void	enable()
{
	asm("sti");
}

void	disable()
{
	asm("cli");
}

